// 引入 request 文件
import request from '@/utils/base.js'
// type判断请求的IP

// 获取订单列表
export function getAllInTotal(data) {
	return request({
		method: 'POST',
		data,
		url: '/ai/order/AllInTotal',
		type: 1
	})
}

// 获取发票总金额
export function getAmount(data) {
	return request({
		method: 'POST',
		data,
		url: '/ai/order/getTotalInvoiceAmount',
		type: 1
	})
}

// 未开发票列表
export function getBillList(data) {
	return request({
		method: 'POST',
		data,
		url: '/ai/order/FundFlowList',
		type: 1
	})
}

// 已开发票列表
export function getDrawList(data) {
	return request({
		method: 'POST',
		data,
		url: '/ai/order/getInvoicedAlreadyIssuedList',
		type: 1
	})
}

// 工单列表
export function getWorkList(data) {
	return request({
		method: 'POST',
		data,
		url: '/ai/work/WorkList',
		type: 1
	})
}

// 工单聊天记录
export function getWorkContentList(data) {
	return request({
		method: 'POST',
		data,
		url: '/ai/work/WorkContentList',
		type: 1
	})
}

// 工单回复
export function WorkContentAdd(data) {
	return request({
		method: 'POST',
		data,
		url: '/ai/work/WorkContentAdd',
		type: 1
	})
}

// 催单
export function WorkReminder(data) {
	return request({
		method: 'POST',
		data,
		url: '/ai/work/WorkRemind',
		type: 1
	})
}

// 结单
export function WorkStatement(data) {
	return request({
		method: 'POST',
		data,
		url: '/ai/work/WorkConfigRm',
		type: 1
	})
}

// 发起工单
export function WorkAdd(data) {
	return request({
		method: 'POST',
		data,
		url: '/ai/work/WorkAdd',
		type: 1
	})
}

// 评价
export function workEvaluate(data) {
	return request({
		method: 'POST',
		data,
		url: '/ai/work/WorkEvaluate',
		type: 1
	})
}

// 消费后获取用户信息
export function getRefreshUser(data) {
	return request({
		method: 'POST',
		data,
		url: '/ai/draw/info',
		type: 1
	})
}

// 充值
export function recharge(data) {
	return request({
		method: 'POST',
		data,
		url: '/ai/order/createOrderXcx',
		type: 1
	})
}

// 获取小程序支付密钥
export function getWechatKey(data) {
	return request({
		method: 'POST',
		data,
		url: '/ai/draw/getWeChatKay',
		type: 1
	})
}

// 获取套餐列表
export function getConfigList(data) {
	return request({
		method: 'POST',
		data,
		url: '/ai/draw/configList',
		type: 1
	})
}

// 获取支付单号
export function getCreateOrder(data) {
	return request({
		method: 'POST',
		data,
		url: '/ai/draw/createOrder',
		type: 1
	})
}

// 修改用户信息
export function editUserInfo(data) {
	return request({
		method: 'POST',
		data,
		url: '/ai/user/upUserInfo',
		type: 2
	})
}

// 绑定微信小程序或者微信公众号
export function bindWxApplet(data) {
	return request({
		method: 'POST',
		data,
		url: '/oauth/Authorize/bindingUserWxApplet',
		type: 3
	})
}

// 支付
export function payMoney(data) {
	uni.showLoading({
		title: '请稍后...'
	})
	return new Promise((resolve, rejcet)=>{
			getWechatKey(data).then(key => {
				console.log('key', key)
				if(key.code == '201004') {
					uni.login({
						success:(res)=> {
							let param = {
								wxid: 'gh_85adb5b7352f',
								appid: '073820240321128890',
								wx_code: res.code,
								openid: uni.getStorageSync('openid')
							}
							bindWxApplet(param).then(res => {
								if(res.errcode && res.errcode == 200) {
									getWechatKey(data).then(pay => {
										console.log('绑定成功', pay)
										const tt = pay.data.package
										const {
											appId,
											nonceStr,
											paySign,
											signType,
											timeStamp
										} = pay.data;
										uni.hideLoading()
										uni.requestPayment({
											provider: 'wxpay',
											timeStamp: timeStamp + '',
											nonceStr: nonceStr,
											package: tt,
											signType: signType,
											paySign: paySign,
											success: function(res) {
												console.log('success:' + JSON.stringify(res));
												uni.showToast({
													title: '购买成功',
													icon: 'none'
												})
												setTimeout(() => {
													uni.navigateBack()
												}, 2000)
											},
											fail: function(err) {
												console.log('fail:' + JSON.stringify(err));
											}
										});
									})
								} else {
									console.log('绑定失败', res)
									uni.showToast({
										title: '绑定微信用户失败，无法支付！请联系客服',
										icon: 'none',
										duration: 5000
									});
									uni.hideLoading()
								}
								console.log('绑定', res)
								uni.hideLoading()
							})
						}
					})
					return
				}
				
				const tt = key.data.package
				const {
					appId,
					nonceStr,
					paySign,
					signType,
					timeStamp
				} = key.data;
				uni.hideLoading()
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: timeStamp + '',
					nonceStr: nonceStr,
					package: tt,
					signType: signType,
					paySign: paySign,
					success: function(res) {
						console.log('success:' + JSON.stringify(res));
						uni.showToast({
							title: '购买成功',
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 2000)
					},
					fail: function(err) {
						console.log('fail:' + JSON.stringify(err));
					}
				});
			
			})
		// })
	})
}